package me.keiwu.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import me.keiwu.admin.rpc.ChallengeRpc;
import me.keiwu.pub.data.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by kei on 28/11/2017.
 */
@RestController
@RequestMapping("/api/challenge")
public class ChallengeController {


    @Autowired
    private ChallengeRpc challengeRpc;


    @ResponseBody
    @RequestMapping("/list")
    public Response listChallenge(@RequestParam(required = false) Integer state,
                                  @RequestParam(required = false) String afterDate,
                                  @RequestParam(required = false) String beforeDate,
                                  @RequestParam(required = false) String name,
                                  @RequestParam(defaultValue = "0") Integer page,
                                  @RequestParam(defaultValue = "10") Integer size) {
        JSONObject obj = JSON.parseObject(challengeRpc.listChallenges(state, afterDate, beforeDate, name, page, size));
        return Response.ok(obj);
    }

    @ResponseBody
    @RequestMapping("/list/starting")
    public Response listStartingChallenge(@RequestParam(defaultValue = "0") Integer page,
                                          @RequestParam(defaultValue = "10") Integer size) {
        JSONObject obj = JSON.parseObject(challengeRpc.listStartingChallenges(page, size));
        return Response.ok(obj);
    }


    @ResponseBody
    @RequestMapping("/close")
    public Response closeChallenge(Long challengeId) {
        return Response.ok(challengeRpc.closeChallenge(challengeId));
    }

    @ResponseBody
    @RequestMapping("/reopen")
    public Response reopenChallenge(Long challengeId) {
        return Response.ok(challengeRpc.reopenChallenge(challengeId));
    }

    @ResponseBody
    @RequestMapping("/findByUserPage")
    public Response findByUserPage(Long userId, int page, int size) {
        JSONObject obj = JSON.parseObject(challengeRpc.findAllUserChallenge(userId, page, size));
        return Response.ok(obj);
    }

}
